package com.zis.auction.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zis.auction.dao.AuctBidDao;
import com.zis.auction.model.AuctBid;
import com.zis.auction.service.IAuctBidInter;
import com.zis.base.impl.BaseInterImpl;
import com.zis.common.AuctProperties;
import com.zis.common.RandomUtils;
import com.zis.core.Constant;
import com.zis.core.CustomProperties;
import com.zis.core.ResponseEntity;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2017/12/28
 */
@Service
@Transactional("txManager")
public class AuctBidInterImpl extends BaseInterImpl<AuctBid> implements IAuctBidInter {

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private AuctBidDao auctBidDao;

    @Autowired
    @Qualifier("auctProperties")
    private AuctProperties auctProperties;


    @Override
    public String insert(AuctBid auctBid) throws Exception {
        auctBid.setCreateDate(new Date());
        auctBid.setBidDate(new Date());
        if(auctBid.getAreaOptions().length > 0){
            auctBid.setAreaProvince(auctBid.getAreaOptions()[0]);
            auctBid.setAreaCity(auctBid.getAreaOptions()[1]);
            auctBid.setAreaCounty(auctBid.getAreaOptions()[2]);
        }
        if(auctBid.getBidOptions().length > 0){
            auctBid.setBidProvince(auctBid.getBidOptions()[0]);
            auctBid.setBidCity(auctBid.getBidOptions()[1]);
            auctBid.setBidCounty(auctBid.getBidOptions()[2]);
        }
        //初始化时候设置一个随机数字
        int focus = RandomUtils.randomInt(10, 1) * 12;
        int hits = RandomUtils.randomInt(10,5) * 35 + focus;
        auctBid.setFocus(focus);//初始关注人数
        auctBid.setHits(hits);//初始浏览数量
        int num = sqlManager.insert(auctBid);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String update(AuctBid auctBid) throws Exception {
        auctBid.setUpdateDate(new Date());
        auctBid.setBidImg(auctBid.getBidImg().replaceAll(auctProperties.getFileUrl(), ""));
        if(auctBid.getAreaOptions().length > 0){
            auctBid.setAreaProvince(auctBid.getAreaOptions()[0]);
            auctBid.setAreaCity(auctBid.getAreaOptions()[1]);
            auctBid.setAreaCounty(auctBid.getAreaOptions()[2]);
        }
        if(auctBid.getBidOptions().length > 0){
            auctBid.setBidProvince(auctBid.getBidOptions()[0]);
            auctBid.setBidCity(auctBid.getBidOptions()[1]);
            auctBid.setBidCounty(auctBid.getBidOptions()[2]);
        }
        int num = sqlManager.updateTemplateById(auctBid);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String delete(String id) throws Exception {
        int num = sqlManager.deleteById(AuctBid.class, id);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public AuctBid selectById(String id) throws Exception {
        AuctBid auctBid = auctBidDao.findById(id);
        int focus = auctBid.getFocus();
        if (focus == 0) {
            focus = RandomUtils.randomInt(10, 1) * 12;
        } else {
            focus = focus * 3;
        }
        int hits = auctBid.getHits();
        if (hits == 0) {
            hits = RandomUtils.randomInt(10,5) * 35 + focus;
        } else {
            hits = hits * 5 + focus;
        }
        auctBid.setFocus(Integer.valueOf(focus));
        auctBid.setHits(Integer.valueOf(hits));
        return auctBid;
    }

    @Override
    public List<AuctBid> findByCondition(AuctBid auctBid) throws Exception {
        return sqlManager.template(auctBid);
    }

    @Override
    public String findIndex() throws Exception {
        // 人气推荐
        List<AuctBid> hotList = auctBidDao.findBySuggest("suggest_02", 0, 3);
        // 优质资产
        List<AuctBid> bestList = auctBidDao.findBySuggest("suggest_03", 0, 3);
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("hotList", hotList);
        jsonObj.put("bestList", bestList);
        return ResponseEntity.createNormalJsonResponse(jsonObj);
    }

    @Override
    public String findAll(Integer page, Integer pageSize) throws Exception {
        List<AuctBid> dataList = auctBidDao.findBySuggest(null, page, pageSize);
        if (dataList!=null&&dataList.size()>0) {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("dataList", dataList);
            jsonObj.put("page", page);
            return ResponseEntity.createNormalJsonResponse(jsonObj);
        }
        return ResponseEntity.createNULLJsonResponse("没有数据～");
    }

    @Override
    public String findBySuggest(String suggest, Integer page, Integer pageSize) {
        List<AuctBid> dataList = auctBidDao.findBySuggest(suggest, page, pageSize);
        if (dataList!=null&&dataList.size()>0) {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("dataList", dataList);
            jsonObj.put("page", page);
            return ResponseEntity.createNormalJsonResponse(jsonObj);
        }
        return ResponseEntity.createNULLJsonResponse("没有数据～");
    }

    @Override
    public String updateHits(String abdId) throws Exception {
        int num = auctBidDao.updateHits(abdId);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String updateFocus(String abdId) throws Exception {
        int num = auctBidDao.updateFocus(abdId);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }
}
